﻿using cma.cimiss;
using cma.cimiss.client;
using Surfer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SXMeteorological.Data.Utility
{
    public class Render
    {
        //插值文件
        private string _interpolationDatFilePath;
        //行政区划范围
        private double _xMin;
        private double _xMax;
        private double _yMin;
        private double _yMax;
        //参数
        private Dictionary<string, string> _paraments = new Dictionary<string, string>();
        //接口名称
        private string interfaceName = "getSurfEleByTime";

        private DataQueryClient _dataQueryClient = new DataQueryClient();
        public Render()
        {
        }
        public Render(string interpolationDatFilePath,double xmin,double xmax,double ymin,double ymax,string time)
        {
          
            _interpolationDatFilePath = interpolationDatFilePath;
            _xMin = xmin;
            _yMin = ymin;
            _xMax = xmax;
            _yMax = ymax;
            _paraments.Add("dataCode", "SURF_CHN_MUL_HOR");
            _paraments.Add("elements", "Station_ID_C,PRE_1h");
            _paraments.Add("times",time);
        }

        //获取要素数据 转成dat文件
        public void  GetDatFileData(string interfaceId)
        {
             interfaceId = "getSurfEleByTime";
            string datFile = string.Empty;
            
                DataQueryClient client = new   DataQueryClient();

            /*   2.3 接口参数，多个参数间无顺序 */

              Dictionary<String, String> params1 = new Dictionary<String,String>();

            // 必选参数

              params1.Add("dataCode", "SURF_CHN_MUL_HOR"); // 资料代码

              params1.Add("elements",    "Station_ID_C,PRE_1h");// 检索要素：站号、小时降水

              params1.Add("times", "20150617000000"); // 检索时间

            // 可选参数

              params1.Add("orderby", "Station_ID_C:ASC"); // 排序：按照站号从小到大

            //   params.put("limitCnt", "10") ; // 返回最多记录数： 10

            /*   2.4 返回文件的格式 */

              String dataFormat = "dat";

            /*   2.5 文件的本地全路径 */

              String savePath = "getSurfEleByTime.dat";

            /*   2.6 返回文件的描述对象 */

              RetFilesInfo retFilesInfo = new RetFilesInfo();

            /* 3.   调用接口 */
            try

            {
                  // 初始化接口服务连接资源
                  client.initResources();

                  // 调用接口
                  int rst = client.callAPI_to_saveAsFile(CIMISSPara._uerID, CIMISSPara._passWord, interfaceId,
                          params1, dataFormat, savePath, retFilesInfo);
            }

            catch (Exception e)
            {

            }
              finally

            {

                  // 释放接口服务连接资源

                  client.destroyResources();

              }       
        }
        /// <summary>
        /// 插值
        /// </summary>
        static void Interpolation(string datFilePath ,double xMin,double xMax,double yMin,double yMax)
        {
            Application app = new Application();
            app.GridData2(DataFile: datFilePath + "data.dat",          //数据文件地址
                xCol: 1,                                        //x为第一列数据
                yCol: 2,                                        //y为第二列数据
                zCol: 3,                                        //z为第三列数据
                DupMethod: Surfer.SrfDupMethod.srfDupNone,
                //xMin: 105.489060,                               //x最小值
                //xMax: 111.246800,                               //x最大者
                //yMin: 31.712748,                                //y最小值
                //yMax: 39.582336,   
                 xMin: xMin,
                 xMax: xMax,
                 yMin: yMin,
                 yMax: yMax,//y最大值
                Algorithm: Surfer.SrfGridAlgorithm.srfInverseDistance,  //插值算法反距离加权
                //NumCols: (111.246800 - 105.489060) / 0.01,      //x方向插值数据量
                //NumRows: (39.582336 - 31.712748) / 0.01,        //y方向插值数据量
                 NumCols: (xMax - xMin) / 0.01,      //x方向插值数据量
                NumRows: (yMax - yMin) / 0.01,
                OutGrid: datFilePath + "grid.grd",                     //返回文件为gridfile
                OutFmt: Surfer.SrfGridFormat.srfGridFmtAscii);  //返回文件编码为Ascii
            app.Quit();
            System.GC.Collect(System.GC.GetGeneration(app));
        }
        /// <summary>
        /// 渲染
        /// </summary>
        public static void RenderMap()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            Application app = new Application();
            app.Visible = false;
            app.Caption = @"陕西省自动站雨量等值线图";
            app.GridBlank(path + @"\grid.grd", path + @"\xian.bln", path + @"\xian.grd", SrfGridFormat.srfGridFmtAscii);
            Surfer.IDocuments Docs = app.Documents;
            Surfer.IPlotDocument PlotDoc = (Surfer.IPlotDocument)Docs.Add(SrfDocTypes.srfDocPlot);
            Surfer.IMapFrame MapF = PlotDoc.Shapes.AddContourMap(path + @"\xian.grd");
            PlotDoc.Shapes.AddBaseMap(path + @"\xian.bln", "");

            //显示等值线上的数值
            IPostMap PostMap1 = (IPostMap)PlotDoc.Shapes.AddPostMap(path + @"\dataCounrty.dat", 1, 2, 3, 0, 0).Overlays.Item(1);
            PostMap1.LabelPos = SrfPostPosType.srfPostPosAbove;
            PostMap1.LabelFont.Face = @"宋体";
            PostMap1.LabelFont.Size = 9;
            PostMap1.LabelFont.Bold = false;
            PostMap1.Symbol.Index = 11;
            PostMap1.Symbol.Color = srfColor.srfColorBlue;
            PostMap1.Symbol.Size = 0.04;
            //等值线填充
            Surfer.IContourMap ContourMap = (Surfer.IContourMap)MapF.Overlays.Item(1);

            ContourMap.FillContours = true;//添加颜色填充
            ContourMap.ShowColorScale = true;//图例显示

            //加载系统颜色

            // ContourMap.FillForegroundColorMap.LoadFile(@"C:\Program Files\Golden Software\Surfer 12\ColorScales\Rainbow.clr");
            double[] CustomLevels = new double[6];
            CustomLevels[0] = 0.1;
            CustomLevels[1] = 9.9;
            CustomLevels[2] = 24.9;
            CustomLevels[3] = 99.9;
            CustomLevels[4] = 200;
            CustomLevels[5] = 250;

            Surfer.ILevels Levels = ContourMap.Levels;
            Levels.Set(CustomLevels);

            Levels.Item(1).Fill.ForeColorRGBA.Red = 165;
            Levels.Item(1).Fill.ForeColorRGBA.Green = 243;
            Levels.Item(1).Fill.ForeColorRGBA.Blue = 141;
            //Levels.Item(1).Fill.ForeColorRGBA.Alpha =180;

            Levels.Item(2).Fill.ForeColorRGBA.Red = 153;
            Levels.Item(2).Fill.ForeColorRGBA.Green = 210;
            Levels.Item(2).Fill.ForeColorRGBA.Blue = 202;
            // Levels.Item(2).Fill.ForeColorRGBA.Alpha = 180;

            Levels.Item(3).Fill.ForeColorRGBA.Red = 155;
            Levels.Item(3).Fill.ForeColorRGBA.Green = 188;
            Levels.Item(3).Fill.ForeColorRGBA.Blue = 232;
            //Levels.Item(3).Fill.ForeColorRGBA.Alpha = 180;

            Levels.Item(4).Fill.ForeColorRGBA.Red = 107;
            Levels.Item(4).Fill.ForeColorRGBA.Green = 157;
            Levels.Item(4).Fill.ForeColorRGBA.Blue = 225;
            //Levels.Item(4).Fill.ForeColorRGBA.Alpha =180;

            Levels.Item(5).Fill.ForeColorRGBA.Red = 59;
            Levels.Item(5).Fill.ForeColorRGBA.Green = 126;
            Levels.Item(5).Fill.ForeColorRGBA.Blue = 219;
            // Levels.Item(5).Fill.ForeColorRGBA.Alpha = 180;

            Levels.Item(6).Fill.ForeColorRGBA.Red = 43;
            Levels.Item(6).Fill.ForeColorRGBA.Green = 92;
            Levels.Item(6).Fill.ForeColorRGBA.Blue = 194;
            //  Levels.Item(6).Fill.ForeColorRGBA.Alpha = 180;    

            ContourMap.SmoothContours = SrfConSmoothType.srfConSmoothHigh;
            for (int i = 0; i < ContourMap.Levels.Count; i++)
            {
                Levels.Item(i + 1).ShowLabel = false;                 //
                Levels.Item(i + 1).ShowHach = true;                 //
                Levels.Item(i + 1).Line.Style = "Invisible";         //不显示线

            }
            //ContourMap.Levels.LoadFile(path + @"\colorlev\" + colorfile);
            //覆盖所有地图

            PlotDoc.Shapes.SelectAll();

            Surfer.IMapFrame MapFrame = PlotDoc.Selection.OverlayMaps();

            MapFrame.Axes.Item(1).Visible = false;
            MapFrame.Axes.Item(2).Visible = false;

            MapFrame.Axes.Item(3).Visible = false;

            MapFrame.Axes.Item(4).Visible = false;

            PlotDoc.SaveAs(path + @"\Data.srf", "", SrfSaveFormat.srfSaveFormatDat);
            string strWH = string.Format("width = {0:f0}, height = {1:f0}, KeepAspect = 1, ColorDepth = 32", 800, 900);//设置输出图片的高度和宽度
            PlotDoc.Export2(path + "Image.png", SelectionOnly: false, Options: strWH, FilterId: "png");//设置输出图片格式名

            app.Quit();

        }
    }
}
